package com.lanlian.cayman.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.UpdateVersionDialog;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.AboutInfo;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqAbout;
import com.lanlian.commonlib.bean.ReqUpdateVersion;
import com.lanlian.commonlib.bean.RespAbout;
import com.lanlian.commonlib.bean.RespUpdateVersion;
import com.lanlian.commonlib.bean.UpdateVersionInfo;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.imagelib.ShowImageUtils;
import com.lanlian.utillib.AppUtils;
import com.lanlian.utillib.DisplayUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 14:25
 *     desc   : 关于狮子
 *     version: 1.0
 * </pre>
 */
public class AboutActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvServiceMobile;
    private TextView mTvEmail;
    private TextView mTvVersion;
    private ImageView mImgLogo;
    private TextView mTvLicense;

    private AboutInfo mAboutInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about, true);
        initView();
        setListeners();
        getAbout();
        mTvVersion.setText(getVersion());
        ViewGroup.LayoutParams layoutParams = mImgLogo.getLayoutParams();
        layoutParams.height = (int) (DisplayUtil.getScreenWidth(this) * 0.5);
        mImgLogo.setLayoutParams(layoutParams);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("关于狮子");
        mTvServiceMobile = findViewById(R.id.tv_service_mobile);
        mTvEmail = findViewById(R.id.tv_email);
        mTvVersion = findViewById(R.id.tv_version);
        mImgLogo = findViewById(R.id.img_logo);
        mTvLicense = findViewById(R.id.tv_license);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvServiceMobile.setOnClickListener(this);
        mTvLicense.setOnClickListener(this);
        mTvVersion.setOnClickListener(this);
    }

    private void getAbout() {
        VolleyRequestUtils.getInstance().about(this.toString(), new ReqAbout(), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                RespAbout respAbout = (RespAbout) response;
                mAboutInfo = respAbout.getData();
                if (mAboutInfo != null) {
                    setAbout();
                }
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
            }
        });
    }

    private void setAbout() {
        mTvEmail.setText(mAboutInfo.getServiceEmail());
        mTvServiceMobile.setText(mAboutInfo.getServiceTelephone());
        setTitle(mAboutInfo.getTitle());
        ShowImageUtils.showImageViewFitCenter(this, mAboutInfo.getLogo(),
                SystemConfig.IMG_DEFAULT_BACKGROUND, mImgLogo);
    }

    private String getVersion() {
        try {
            PackageInfo packInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            return "v " + packInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_version:
                updateVersion();
                break;
            case R.id.tv_service_mobile:
                if (mAboutInfo != null) {
                    diallPhone(mAboutInfo.getServiceTelephone());
                }
                break;
            case R.id.tv_license:
                if (mAboutInfo != null) {
                    startActivity(new Intent(this, WebviewActivity.class)
                            .putExtra("url", mAboutInfo.getLicenseURL())
                            .putExtra("title", "牌照资质"));
                }
                break;
            default:
                break;
        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void diallPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    private void updateVersion() {
        ReqUpdateVersion reqUpdateVersion = new ReqUpdateVersion();
        reqUpdateVersion.setMarketNo("android");
        reqUpdateVersion.setVersionNo(AppUtils.getVersionCode(this));
        VolleyRequestUtils.getInstance().updateVersion(this.toString(), reqUpdateVersion, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                RespUpdateVersion respUpdateVersion = (RespUpdateVersion) response;
                UpdateVersionInfo updateVersionInfo = respUpdateVersion.getData();
                if ("1".equals(updateVersionInfo.getHasNewVersion())) {
                    if ("1".equals(updateVersionInfo.getForceUpdate())) {
                        new UpdateVersionDialog(AboutActivity.this, updateVersionInfo).show();
                    } else {
                        if (updateVersionInfo.getVersionNo() > mDBManager.getTempVersionCode()) {
                            new UpdateVersionDialog(AboutActivity.this, updateVersionInfo).show();
                        }
                    }
                } else {
                    showToast("已经是最新版本！");
                }
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
            }
        });
    }

}
